package com.mryan.esjd.utils;

import com.mryan.esjd.pojo.ParseJD;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * @description：TODO
 * @Author MRyan
 * @Date 2020/6/23 20:56
 * @Version 1.0
 */
@Component
public class HtmlParseUtil {


    public List<ParseJD> getParseJD(String keywords) throws IOException {
        List<ParseJD> lists = new ArrayList<>();
        String url = "https://search.jd.com/Search?keyword=" + keywords + "&enc=utf-8";
        Connection connection = Jsoup.connect(url);
        connection.header("User-Agent", "Mozilla/5.0 (X11; Linux x86_64; rv:32.0) Gecko/ 20100101 Firefox/32.0");
        Document document = connection.get();
        Element select = document.getElementById("J_goodsList");
        Elements li = select.getElementsByTag("li");
        for (Element e : li) {
            String img = e.getElementsByTag("img").eq(0).attr("abs:src");
            String price = e.getElementsByClass("p-price").eq(0).text();
            String title = e.getElementsByClass("p-name").eq(0).text();
            ParseJD parseJD = new ParseJD(title, img, price);
            lists.add(parseJD);
        }
        return lists;
    }
}
